//
//  RBMyWalletVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/2.
//

#import "RBMyWalletVC.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import "RanTool.h"
#import "UIViewController+RBAdd.h"
#import "RBMyWalletDetailVC.h"
#import "RBWalletOtherChargeVC.h"
#import "RBMyWalletResponse.h" //model

@interface RBMyWalletVC ()

@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)IBOutlet UIView *topView;
@property (nonatomic, strong)IBOutlet UILabel *balanceL;
@property (nonatomic, strong)NSMutableArray *amountViews;
@property (nonatomic, assign)NSInteger amountIndex;
@property (nonatomic, strong)NSMutableArray *payWayViews;
@property (nonatomic, assign)NSInteger payWayIndex;

@property (nonatomic, strong) RBMyWalletResponse *response;

@end

@implementation RBMyWalletVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getData];
}

#pragma mark - Private

- (void)setup {
    self.title = @"我的钱包";
    [self.topView zoomSubViewsWithSelf];
    self.topView.height = Zoom(self.topView.height);
}

- (void)fillData {
    //model
    RBMyWalletModel *allModel = self.response.data;
    //
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    //顶部view
    [self.scrollView addView:self.topView withHeight:self.topView.height width:kRBMainScreenWidth top:Zoom(7) bottom:0 left:0 right:0 centerX:0];
    self.balanceL.text = allModel.balance;
    //
    [self.scrollView addView:({
        UIView *view = UIView.new;
        
        UILabel *leftL = [RanTool labelWithFontSize:Zoom(16) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@"请选择充值金额："];
        [view addSubview:leftL];
        [leftL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Zoom(16));
            make.top.bottom.mas_equalTo(0);
        }];
        
        UILabel *otherL = [RanTool labelWithFontSize:Zoom(14) weight:1 color:@"#F25B50" align:1 text:@"其他金额"];
        otherL.backgroundColor = [UIColor colorWithHexString:@"#FFEBEA"];
        [otherL rbSetCornerRadius:Zoom(8)];
        [otherL rbAddTapGesWithTarget:self action:@selector(otherAmountViewTapped)];
        [view addSubview:otherL];
        [otherL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(Zoom(-16));
            make.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(Zoom(89));
            make.height.mas_equalTo(Zoom(32));
        }];
        
        view;
    }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(16) bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        
        CGFloat itemW = Zoom(110);
        CGFloat itemH = Zoom(62);
        CGFloat xPadding = Zoom(6);
        CGFloat yPadding = Zoom(10);
        CGFloat totalH = 0;
        for (RBMyWalletListModel *model in allModel.recharge_set) {
            NSInteger index = [allModel.recharge_set indexOfObject:model];
            NSInteger row = index / 3;
            NSInteger column = index % 3;
            CGFloat x = column*(itemW + xPadding);
            CGFloat y = row*(itemH + yPadding);
            UIView *amountView = [self amountViewWithAmount:model.amount discount:model.paid];
            amountView.tag = index;
            [amountView rbAddTapGesWithTarget:self action:@selector(amountViewTapped:)];
            [view addSubview:amountView];
            amountView.frame = CGRectMake(x, y, itemW, itemH);
            [self.amountViews addObject:amountView];
            totalH = y + itemH;
        }
        
        UIView *pview = UIView.new;
        pview.backgroundColor = UIColor.clearColor;
        [view addSubview:pview];
        [pview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(1);
            make.height.mas_equalTo(totalH);
        }];
        
        view;
    }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth - Zoom(16) top:Zoom(15) bottom:0 left:Zoom(16) right:0 centerX:0];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        
        UILabel *leftL = [RanTool labelWithFontSize:Zoom(16) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@"支付方式："];
        [view addSubview:leftL];
        [leftL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Zoom(16));
            make.top.bottom.mas_equalTo(0);
        }];
        
        view;
    }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(36) bottom:0 left:0 right:0 centerX:0];
    for(NSInteger i = 0; i < 2; i++)
    {
        [self.scrollView addView:[self payWayWithIndex:i] withHeight:Zoom(50) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
    }
    [self.scrollView addView:({
        UILabel *view = [RanTool labelWithFontSize:15 weight:0 color:[UIColor whiteColor] align:1 text:@"立即充值"];
        view.backgroundColor = [UIColor colorWithHexString:@"#518CFF"];
        [view rbSetCornerRadius:Zoom(22)];
        view;
    }) withHeight:Zoom(44) width:Zoom(350) top:Zoom(80) bottom:Zoom(50) left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    [self selectAmountAtIndex:0];
    [self selectPayWayAtIndex:0];
}

- (void)selectAmountAtIndex: (NSInteger)index {
    self.amountIndex = index;
    for (UIView *itemView in self.amountViews) {
        if (itemView.tag == index) {
            itemView.backgroundColor = [UIColor colorWithHexString:@"#FFEBEA"];
            [itemView rbSetBorderWitdh:Zoom(1) borderColor:[UIColor colorWithHexString:@"#F25B50"]];
            [itemView viewWithTag:101].hidden = NO;
            [itemView viewWithTag:102].hidden = YES;
        } else {
            itemView.backgroundColor = UIColor.whiteColor;
            [itemView rbSetBorderWitdh:Zoom(1) borderColor:[UIColor colorWithHexString:@"#F2F2F2"]];
            [itemView viewWithTag:102].hidden = NO;
            [itemView viewWithTag:101].hidden = YES;
        }
    }
}

- (void)selectPayWayAtIndex: (NSInteger)index {
    self.payWayIndex = index;
    for (UIView *itemView in self.payWayViews) {
        UIImageView *imgView = (UIImageView *)[itemView viewWithTag:33];
        imgView.image = itemView.tag == index? [UIImage imageNamed:@"addressListDefault"]: [UIImage imageNamed:@"groupPayUnsel"];
    }
}

#pragma mark - Network

- (void)getData {
    LCWeakSelf;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"user_id"] = [RBAccountManager getUserId];
    } path:_Recharge_get post:YES log:YES responseClass:RBMyWalletResponse.class desView:self.view showHud:!self.response showPlaceHolder:NO sucBlock:^(id data) {
        weakSelf.response = data;
        [weakSelf fillData];
    } failBlock:^(RBMyWalletResponse *data) {
        [weakSelf.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Event

- (IBAction)detailBtnTapped:(id)sender
{
    RBMyWalletDetailVC *vc = RBMyWalletDetailVC.new;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)otherAmountViewTapped
{
    RBWalletOtherChargeVC *vc = RBWalletOtherChargeVC.new;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)amountViewTapped: (UITapGestureRecognizer *)ges
{
    [self selectAmountAtIndex:ges.view.tag];
}

- (void)payWayViewTapped: (UITapGestureRecognizer *)ges
{
    [self selectPayWayAtIndex:ges.view.tag];
}

#pragma mark - Setter

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _scrollView;
}

- (UIView *)amountViewWithAmount: (NSString *)amount discount: (NSString *)discount
{
    UIView *view = UIView.new;
    [view rbSetCornerRadius:Zoom(8)];
    
    UILabel *topL = UILabel.new;
    topL.tag = 101;
    topL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(18)] color:[UIColor colorWithHexString:@"#F25B50"] string:amount];
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(11)] color:[UIColor colorWithHexString:@"#F25B50"] string:@"元"];
    }];
    [view addSubview:topL];
    [topL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(Zoom(14));
    }];
    
    UILabel *topL1 = UILabel.new;
    topL1.tag = 102;
    topL1.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(18)] color:[UIColor colorWithHexString:@"#333333"] string:amount];
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(11)] color:[UIColor colorWithHexString:@"#333333"] string:@"元"];
    }];
    [view addSubview:topL1];
    [topL1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(Zoom(14));
    }];
    
    UILabel *bottomL = [RanTool labelWithFontSize:Zoom(11) weight:0 color:@"#BDBDBD" align:0 text:[NSString stringWithFormat:@"售价: %@元", discount]];
    [view addSubview:bottomL];
    [bottomL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-Zoom(13));
        make.centerX.mas_equalTo(0);
    }];
    
    return view;
}

- (UIView *)payWayWithIndex: (NSInteger)index
{
    NSArray *imgs = @[
        @"walletWechat",
        @"walletAli",
    ];
    NSArray *names = @[
        @"微信",
        @"支付宝",
    ];
    UIView *view = UIView.new;
    [self.payWayViews addObject:view];
    view.tag = index;
    [view rbAddTapGesWithTarget:self action:@selector(payWayViewTapped:)];
    
    UIImageView *imgView = [UIImageView.alloc initWithImage:[UIImage imageNamed:imgs[index]]];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(16));
        make.width.height.mas_equalTo(Zoom(18));
        make.centerY.mas_equalTo(0);
    }];
    
    UILabel *nameL = [RanTool labelWithFontSize:Zoom(15) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:names[index]];
    [view addSubview:nameL];
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imgView.mas_right).offset(Zoom(9));
        make.centerY.mas_equalTo(0);
    }];
    
    UIImageView *selImgView = UIImageView.new;
    selImgView.tag = 33;
    [view addSubview:selImgView];
    [selImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-Zoom(16));
        make.width.height.mas_equalTo(Zoom(15));
    }];
    
    return view;
}


- (NSMutableArray *)amountViews
{
    if (!_amountViews)
    {
        self.amountViews = NSMutableArray.array;
    }
    return _amountViews;
}

- (NSMutableArray *)payWayViews
{
    if (!_payWayViews)
    {
        self.payWayViews = NSMutableArray.array;
    }
    return _payWayViews;
}

@end
